package com.xingmang.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingmang.common.constant.PageEntity;
import com.xingmang.common.constant.ResponseData;
import com.xingmang.common.constant.ResultBean;
import com.xingmang.common.dto.admin.OaCategoryInfoDTO;
import com.xingmang.common.dto.admin.OaCategorySaveDTO;
import com.xingmang.common.dto.admin.OaCategoryUpdateDTO;
import com.xingmang.common.dto.base.BasePageParam;
import com.xingmang.common.utils.TokenUtils;
import com.xingmang.common.vo.admin.OaCategoryPageVO;
import com.xingmang.common.vo.admin.OaCategoryVO;
import com.xingmang.db.entity.OaCategory;
import com.xingmang.db.entity.OaFlow;
import com.xingmang.db.service.OaCategoryDBService;
import com.xingmang.db.service.OaFlowDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/6/30 9:26
 */
@Service
@Slf4j
public class OaCategoryService {
    @Resource
    private OaCategoryDBService oaCategoryDBService;

    @Resource
    private OaFlowDBService oaFlowDBService;

    public ResultBean<PageEntity<OaCategoryPageVO>> page(BasePageParam dto) {
        Page<OaCategory> page = oaCategoryDBService.page(new Page<>(dto.getPagenum(), dto.getPagesize()), new LambdaQueryWrapper<OaCategory>().orderByAsc(OaCategory::getCategoryOrder));
        List<OaCategoryPageVO> collect = page.getRecords().stream().map(oaCategory -> {
            OaCategoryPageVO vo = BeanUtil.copyProperties(oaCategory, OaCategoryPageVO.class);
            vo.setCategoryId(oaCategory.getId());
            return vo;
        }).collect(Collectors.toList());
        PageEntity<OaCategoryPageVO> pageEntity = new PageEntity<>();
        BeanUtil.copyProperties(page,pageEntity);
        pageEntity.setRecords(collect);
        return ResponseData.success(pageEntity);
    }

    public ResultBean<List<OaCategoryVO>> list() {
        List<OaCategory> list = oaCategoryDBService.list();
        List<OaCategoryVO> collect = list.stream().map(oaCategory -> {
            OaCategoryVO vo = BeanUtil.copyProperties(oaCategory, OaCategoryVO.class);
            vo.setCategoryId(oaCategory.getId());
            return vo;
        }).collect(Collectors.toList());
        return ResponseData.success(collect);
    }

    public ResultBean save(OaCategorySaveDTO dto) {
        Long userId = TokenUtils.getUserId();
//        Long userId = 1L;
        List<OaCategory> list = oaCategoryDBService.list(new LambdaQueryWrapper<OaCategory>().eq(OaCategory::getCategoryName, dto.getCategoryName()));
        if (CollectionUtil.isNotEmpty(list)){
            return ResponseData.error("此分类名称已存在");
        }
        OaCategory oaCategory = BeanUtil.copyProperties(dto, OaCategory.class);
        oaCategory.setUserId(userId);
        oaCategoryDBService.save(oaCategory);
        return ResponseData.success(null);
    }

    public ResultBean update(OaCategoryUpdateDTO dto) {
        OaCategory oaCategory = BeanUtil.copyProperties(dto, OaCategory.class);
        oaCategory.setId(dto.getCategoryId());
        oaCategoryDBService.updateById(oaCategory);
        return ResponseData.success(null);
    }

    public ResultBean delete(OaCategoryInfoDTO dto) {
        long count = oaFlowDBService.count(new LambdaQueryWrapper<OaFlow>().eq(OaFlow::getCategoryId, dto.getCategoryId()));
        if (count>0){
            return ResponseData.error("此分类下有流程，不能删除");
        }
        oaCategoryDBService.removeById(dto.getCategoryId());
        return ResponseData.success(null);
    }
}
